﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using StatisticsBadRatio.Common;
using StatisticsBadRatio.Configs;

namespace StatisticsBadRatio.ViewModel
{
    public class SettingViewModel :ViewModelBase
    {
        #region 属性
        private int QRcodeStartIndex=0;
        /// <summary>
        /// 二维码开始位置
        /// </summary>
        public int QRCodeStartIndex
        {
            get { return QRcodeStartIndex; }
            set { QRcodeStartIndex = value; this.RaisePropertyChanged(() => QRCodeStartIndex); }
        }
        private int QRcodeLength=0;

        public int QRCodeLength
        {
            get { return QRcodeLength; }
            set { QRcodeLength = value; this.RaisePropertyChanged(() => QRCodeLength); }
        }
        private string sourceFileFormat;
        /// <summary>
        /// 源文件格式
        /// </summary>
        public string SourceFileFormat
        {
            get { return sourceFileFormat; }
            set { sourceFileFormat = value; this.RaisePropertyChanged(() => SourceFileFormat); }
        }
        private ObservableCollection<string> testItems;
        /// <summary>
        /// 测试项集合
        /// </summary>
        public ObservableCollection<string> TestItems
        {
            get { return testItems; }
            set { testItems = value; this.RaisePropertyChanged(() => TestItems); }
        }
        private string testItem;
        /// <summary>
        /// 测试项
        /// </summary>
        public string TestItem
        {
            get { return testItem; }
            set { testItem = value; this.RaisePropertyChanged(() => TestItem); }
        }

        #endregion

        #region 命令
        private RelayCommand loadedCmd;
        /// <summary>
        /// 页面加载
        /// </summary>
        public RelayCommand LoadedCmd
        {
            get {
                loadedCmd = loadedCmd ?? new RelayCommand(LoadedPage);
                return loadedCmd; }
            set { loadedCmd = value; }
        }
        private RelayCommand addTestItemCmd;

        public RelayCommand AddTestItemCmd
        {
            get {
                addTestItemCmd = addTestItemCmd ?? new RelayCommand(AddTestItem);
                return addTestItemCmd; }
            set { addTestItemCmd = value; }
        }
        private RelayCommand cancelCmd;

        public RelayCommand CancelCmd
        {
            get {
                cancelCmd = cancelCmd ?? new RelayCommand(CancleHandler);
                return cancelCmd; }
            set { cancelCmd = value; }
        }
        private RelayCommand confirmCmd;

        public RelayCommand ConfirmCmd
        {
            get {
                confirmCmd = confirmCmd ?? new RelayCommand(ConfirmHandler);
                return confirmCmd; }
            set { confirmCmd = value; }
        }
        private RelayCommand unloadedCmd;

        public RelayCommand UnloadedCmd
        {
            get {
                unloadedCmd = unloadedCmd ?? new RelayCommand(UnloadedPage);
                return unloadedCmd; }
            set { unloadedCmd = value; }
        }
        private RelayCommand<string> lstTestItem_MouseDoubleClick;
        /// <summary>
        /// 列表鼠标双击事件
        /// </summary>
        public RelayCommand<string> LstTestItem_MouseDoubleClick
        {
            get {
                lstTestItem_MouseDoubleClick = lstTestItem_MouseDoubleClick ?? new RelayCommand<string>(LstTestItemMouseDoubleClickHandler);
                return lstTestItem_MouseDoubleClick; }
            set { lstTestItem_MouseDoubleClick = value; }
        }

        #endregion

        #region 方法
        /// <summary>
        /// 加载页面
        /// </summary>
        private void LoadedPage()
        {
            QRCodeStartIndex = Global.AppConfigs.QRCodeStartIndexInFileName;
            QRCodeLength = Global.AppConfigs.QRCodeLength;
            SourceFileFormat = Global.AppConfigs.DataSourceFileSearchFormat;
            string[] arrTestItems = Global.AppConfigs.SearchTestItem.Split('|');
            TestItems = new ObservableCollection<string>();
            foreach (var item in arrTestItems)
            {
                TestItems.Add(item);
            }
        }

        private void LstTestItemMouseDoubleClickHandler(string obj)
        {
            if (TestItems.Contains(obj))
            {
                if (MessageBox.Show("你确定要删除测试项" + obj + "吗？", "软件温馨提示", MessageBoxButton.YesNo, MessageBoxImage.Question)==MessageBoxResult.Yes)
                {
                    TestItems.Remove(obj);
                } ;
            }
        }

        //添加测试项
        private void AddTestItem()
        {
            if ((TestItem??"")=="")
            {
                MessageBox.Show("测试项不能为空,请先输入测试项.","软件温馨提示",MessageBoxButton.OK,MessageBoxImage.Warning);
                return;
            }
            if (TestItems.Contains(TestItem))
            {
                MessageBox.Show("列表中已经包含此测试项,请不要重复录入.", "软件温馨提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            TestItems.Add(TestItem);
        }
        /// <summary>
        /// 确认操作
        /// </summary>
        private void ConfirmHandler()
        {
            if (SourceFileFormat=="")
            {
                MessageBox.Show("源文件格式不能为空.", "软件温馨提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (TestItems.Count<=0)
            {
                MessageBox.Show("测试项不能为空.", "软件温馨提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            ConfigInfo config = new ConfigInfo() {
                QRCodeStartIndexInFileName = QRCodeStartIndex,
                QRCodeLength = QRCodeLength,
                DataSourceFileSearchFormat = SourceFileFormat ?? "",
                SearchTestItem = String.Join("|", TestItems.ToArray())
            };
            BusinessProcess.SerializeToXml<ConfigInfo>(Environment.CurrentDirectory+ "\\AppConfig.cfg", config);
            MessageBox.Show("保存成功!","软件温馨提示!",MessageBoxButton.OK,MessageBoxImage.Information);
            Messenger.Default.Send<ConfigInfo>(config, "config");
        }
        /// <summary>
        /// 取消操作
        /// </summary>
        private void CancleHandler()
        {
            ViewManager.Close("Setting");
        }
        /// <summary>
        /// 卸载页面
        /// </summary>
        private void UnloadedPage()
        {
            Messenger.Default.Unregister<ConfigInfo>(this);//释放信使信息
        }

        #endregion
    }
}
